package com.gjt.thread.Thread;

import javax.xml.bind.DatatypeConverter;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created with Intel IDEA.
 * 这个有点问题
 *
 * @Auther: gjt
 * @Date: 2024/3/20 16:53
 */
public class ReturnDigestThread extends Thread{

    private String fileName;
    private byte[] digest;

    public ReturnDigestThread(String fileName){
        this.fileName=fileName;
    }

    @Override
    public void run() {
        try(FileInputStream in=new FileInputStream(fileName)){
            // 计算一个256位的SHA-2消息摘要 用到的DigestInputStream
            MessageDigest sha = MessageDigest.getInstance("SHA-256");
            DigestInputStream din = new DigestInputStream(in, sha);

            while(din.read() != -1);//读取到最后并计算摘要
            din.close();

            // 获取摘要
             digest = sha.digest();
        }catch (IOException e){
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public byte[] getDigest(){
        return digest;
    }

}
